﻿Imports System.ServiceProcess
Imports System.IO

Public Class Form1
    Dim Datei As String = Application.StartupPath & "\Dienste.txt"
    Dim Dienst(0) As String, counter As Integer = 0, counter2 As Integer = 0, offeneDienste(0) As String
    Dim ExterneAnwendung As New System.Diagnostics.Process(), dienstaktuell As String = ""
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1_Click(Me, e)
        Timer1.Start()
    End Sub

    ''' <summary>
    ''' Ermittelt Windows-Dienste. ComputerName = Sie koennen den Computernamen angeben.
    ''' </summary>
    ''' <param name="ComputerName">Computername oder . für aktuellen Computer</param>
    Private Function EnumWindowsServiceProcess(Optional ByVal ComputerName As String = "") As List(Of String)

        ' ServiceController initialisieren.
        Dim sc() As ServiceController
        ' Liste initialisieren.
        Dim List As New List(Of String)

        Try
            If (ComputerName.Trim() = String.Empty) Then
                sc = ServiceController.GetServices() ' Lokalen PC Name verwenden
            Else
                sc = ServiceController.GetServices(ComputerName) 'Übergebenen PC Namen
            End If
            For Each n As ServiceController In sc
                List.Add(n.DisplayName)
            Next
            Return List
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString(), "Info")
        End Try
        Return Nothing
    End Function


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' Computernamen anzeigen... Nur zum Hinweis oder zur Uebergabe 
        ' an (die) Funktion("EnumWindowsServiceProcess")
        Dim _computername As String = My.Computer.Name()
        Me.Text = "Computer: " & _computername

        ' ListView - Item deklarieren
        Dim Item As ListViewItem = Nothing
        ' Enthaelt das ListView Control Eintraege werden diese entfernt.
        If Me.ListView1.Items.Count > 0 Then Me.ListView1.Clear()

        With Me.ListView1
            Try
                ' Funktion "EnumWindowsServiceProcess" aufrufen und 
                ' Ergebnis an Liste uebergeben.
                Dim List As List(Of String) = _
                  Me.EnumWindowsServiceProcess(System.Environment.MachineName) ' oder _computername
                ' Enthaelt die Liste Eintraege werden diese in einer 
                ' Schleife ausgelesen und an die Variable Item uebergeben.
                If List IsNot Nothing Then
                    For n As Integer = 0 To List.Count - 1
                        Item = New ListViewItem(List(n), 0)
                        ' ListView mit den Werten aus der Variablen Item fuellen.
                        .Items.Add(Item)
                    Next
                Else
                    ' Enthaelt die Liste keine Eintraege wird eine MessageBox angezeigt...
                    MessageBox.Show("Keine Eintraege vorhanden!", "Info")
                End If
            Catch ex As Exception
                ' Entsteht ein Fehler wird dieser abgefangen.
                MessageBox.Show(ex.Message.ToString(), "Info")
            End Try
        End With
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim fs As FileStream = New FileStream(Datei, FileMode.OpenOrCreate, FileAccess.ReadWrite)
        Dim r As StreamReader = New StreamReader(fs)
        Label1.Text = Label1.Text + 1
        Label1.Refresh()

        Do While r.Peek > 0
            Dienst(counter) = r.ReadLine
            counter += 1
            ReDim Preserve Dienst(counter)
        Loop

        For i As Integer = 0 To ListView1.Items.Count - 1
            offeneDienste(counter2) = ListView1.Items(i).ToString
            counter2 += 1
            ReDim Preserve offeneDienste(counter2)
        Next

        Dim serviceToKill As New ServiceController()
        serviceToKill.MachineName = System.Environment.MachineName

        For i As Integer = 0 To UBound(Dienst) - 1
            For x As Integer = 0 To UBound(offeneDienste) - 1
                dienstaktuell = Mid(offeneDienste(x).ToString, InStr(offeneDienste(x).ToString, "{", CompareMethod.Text))
                dienstaktuell = Replace(dienstaktuell, "{", "", 1)
                dienstaktuell = Replace(dienstaktuell, "}", "", 1)
                If Dienst(i) = dienstaktuell Then
                    If ServiceAvailable(dienstaktuell) = True Then
                        serviceToKill.MachineName = System.Environment.MachineName
                        serviceToKill = New ServiceController(dienstaktuell)
                        serviceToKill.Stop()
                    End If
                End If
            Next
        Next
        counter = 0
        counter2 = 0
        Array.Clear(Dienst, 1, UBound(Dienst))
        Array.Clear(offeneDienste, 1, UBound(offeneDienste))
        r.Close()


    End Sub
    Public Function ServiceAvailable(ByVal Dienst As String) As Boolean
        Dim Service As ServiceController = New ServiceController(Dienst)
        Return CBool(Service.Status = ServiceProcess.ServiceControllerStatus.Running)
    End Function

    
    Private Sub buttonEndSelectedService_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonEndSelectedService.Click
        Dim serviceName As String = ListView1.SelectedItems(0).Text
        Dim killService As New ServiceController(serviceName)

        If (killService.Status = ServiceControllerStatus.Running) Then
            killService.Stop()
        End If




    End Sub
End Class



